import * as Cesium from 'cesium';
export function getXbsjWaterMaterial(uniforms?: any) {
    return new Cesium.Material({
        fabric: {
            type: "XbsjWater",
            uniforms: {
                baseWaterImage: '',
                repeatX: 1, //X重复
                repeatY: 1, //Y重复
                flowSpeed: 0,
                baseWaterColor: new Cesium.Color(0.2, 0.3, 0.6, 1.0),
                blendColor: new Cesium.Color(0.0, 1.0, 0.699, 1.0),
                specularMap: Cesium.Material.DefaultImageId,
                normalMap: Cesium.Material.DefaultImageId,
                frequency: 10.0,
                animationSpeed: 0.01,
                amplitude: 1.0,
                specularIntensity: 0.5,
                fadeFactor: 1.0,
                ...uniforms
            },
            source: `// Thanks for the contribution Jonas
                    // http://29a.ch/2012/7/19/webgl-terrain-rendering-water-fog
    
                    uniform sampler2D specularMap;
                    uniform sampler2D normalMap;
                    uniform sampler2D baseWaterImage;
                    uniform vec4 baseWaterColor;
                    uniform vec4 blendColor;
                    uniform float frequency;
                    uniform float animationSpeed;
                    uniform float amplitude;
                    uniform float specularIntensity;
                    uniform float fadeFactor;
                    uniform float repeatX;
                    uniform float repeatY;
                    uniform float flowSpeed;
    
                    czm_material czm_getMaterial(czm_materialInput materialInput)
                    {
                        czm_material material = czm_getDefaultMaterial(materialInput);
    
                        float time = czm_frameNumber * animationSpeed;
                        vec2 st = materialInput.st;
                        st = fract(st * vec2(repeatX, repeatY));
                        vec2 addSt = vec2(flowSpeed,0.0) * (czm_frameNumber / 60.0);
    
                        // fade is a function of the distance from the fragment and the frequency of the waves
                        float fade = max(1.0, (length(materialInput.positionToEyeEC) / 10000000000.0) * frequency * fadeFactor);
    
                        float specularMapValue = texture(specularMap, fract(st - addSt)).r;
    
                        // note: not using directional motion at this time, just set the angle to 0.0;
                        vec4 noise = czm_getWaterNoise(normalMap, fract(st - addSt) * frequency, time, 0.0);
                        vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / amplitude));
    
                        // fade out the normal perturbation as we move further from the water surface
                        normalTangentSpace.xy /= fade;
    
                        // attempt to fade out the normal perturbation as we approach non water areas (low specular map value)
                        normalTangentSpace = mix(vec3(0.0, 0.0, 50.0), normalTangentSpace, specularMapValue);
    
                        normalTangentSpace = normalize(normalTangentSpace);
    
                        // get ratios for alignment of the new normal vector with a vector perpendicular to the tangent plane
                        float tsPerturbationRatio = clamp(dot(normalTangentSpace, vec3(0.0, 0.0, 1.0)), 0.0, 1.0);
    
                        // fade out water effect as specular map value decreases
                        vec4 baseColor = texture(baseWaterImage, fract(st - addSt));
                        baseColor.rgb=mix(baseColor.rgb,baseWaterColor.rgb, baseWaterColor.a);
                        material.alpha = mix(blendColor.a, baseColor.a, specularMapValue) * specularMapValue;
    
                        // base color is a blend of the water and non-water color based on the value from the specular map
                        // may need a uniform blend factor to better control this
                        material.diffuse = mix(blendColor.rgb, baseColor.rgb, specularMapValue);
    
                        // diffuse highlights are based on how perturbed the normal is
                        material.diffuse += (0.1 * tsPerturbationRatio);
    
                        material.diffuse = material.diffuse;
    
                        material.normal = normalize(materialInput.tangentToEyeMatrix * normalTangentSpace);
    
                        material.specular = specularIntensity;
                        material.shininess = 10.0;
    
                        return material;
                    }`
        }
    });
}